package org.cheetah.spring.xml;

import org.cheetah.activities.CallMethod;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

public class CallMethodDefinitionParser extends
		AbstractSingleBeanDefinitionParser {

	@Override
	protected Class<?> getBeanClass(Element element) {
		return CallMethod.class;
	}

	@Override
	protected void doParse(Element element, BeanDefinitionBuilder bean) {
		String objectRef = element.getAttribute("object-ref");
		String object = element.getAttribute("object");
		String objectVar = element.getAttribute("object-var");

		String className = element.getAttribute("class");

		String method = element.getAttribute("method");
		String arguments = element.getAttribute("arguments");
		String resultVar = element.getAttribute("result-var");

		if (StringUtils.hasText(objectRef)) {
			bean.addPropertyReference("objectRef", objectRef);
		}
		if (StringUtils.hasText(object)) {
			bean.addPropertyValue("object", object);
		}
		if (StringUtils.hasText(objectVar)) {
			bean.addPropertyValue("objectVar", objectVar);
		}
		
		if (StringUtils.hasText(className)) {
			bean.addPropertyValue("className", className);
		}

		bean.addPropertyValue("method", method);

		if (StringUtils.hasText(arguments)) {
			bean.addPropertyValue("arguments", arguments);
		}

		if (StringUtils.hasText(resultVar)) {
			bean.addPropertyValue("resultVar", resultVar);
		}
	}
}
